package com.cea.papyrus.core.actions.imports;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;

import com.cea.papyrus.core.actions.selectcopypaste.PapyrusSelectionAction;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.IUMLElementEditPart;

/**
 * Action that imports package
 */
public abstract class ImportPackageAction extends PapyrusSelectionAction {

	/** resource set that contains the resource where the modified Package is */
	protected final ResourceSet resourceSet;
	
	/**
	 * Creates a new ImportLibraryFromRepositoryAction
	 * @param part that part which contains the selection
	 */
	public ImportPackageAction(IWorkbenchPart part) {
		super(part);
		ModelManager manager = (ModelManager)part.getAdapter(ModelManager.class);
		resourceSet = manager.getResourceSet();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean calculateEnabled() {
		// look the selection.
		// if one of the element is a package and is not an imported package, it is enabled
		return(getSelectedPackage()!=null);
	}
	
	/**
	 * returns the first selected package in the collection of selected elements
	 * @return the first selected package in the collection of selected elements
	 */
	protected Package getSelectedPackage() {
		final List<Package> packages =getSelectedPackages(true);
		return (packages.size()>0) ? packages.get(0) : null;
	}
	/**
	 * Returns the collection of selected packages 
	 * @param filterImports <code>true</code> if imported elements are counted as valid results
	 * @return the collection of selected packages 
	 */
	protected List<Package> getSelectedPackages(boolean filterImports) {
		List<Package> selectedPackage = new ArrayList<Package>();
		Iterator<Object> it = getSelectedObjects().iterator();
		while (it.hasNext()) {
			Object object =  it.next();
			if(object instanceof IUMLElementEditPart) {
				IUMLElementEditPart<Element> editPart = (IUMLElementEditPart<Element>)object;
				if(editPart.getUmlElement() instanceof Package) {
					if(editPart.isElementImport()) {
						if(!filterImports) {
							selectedPackage.add((Package)editPart.getUmlElement());
						}
					} else {
						selectedPackage.add((Package)editPart.getUmlElement());
					}
				}
			}
		}
		return selectedPackage;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void run () {
		execute(createCommand(editingDomain));
	}
	
	/**
	 * Creates the command that will import packages
	 * @param editingDomain the editing domain used by the emf command that imports the package
	 * @return the command that imports the package
	 */
	public abstract Command createCommand(EditingDomain editingDomain);
	
}